package com.dongdongshop_util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.Map;

public class SendSmsDemo {

    // 设置鉴权参数，初始化客户端
    static DefaultProfile profile = DefaultProfile.getProfile(
            "cn-qingdao",// 地域ID
            "LTAI4G64FQi4fg7X94gJnCS8",
                "JW6adh0hkOWgERxATyjaILbt2Oc3UD");// 您的AccessKey Secret
    static IAcsClient client = new DefaultAcsClient(profile);

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }


    /**
     * 添加短信模板
     */
    public static void main(String[] args) {
        double random = Math.random();
        System.out.println(random);
    }

    /**
     * 发送短信，，，阿里大于
     */
    public static String sendSms(String phoneNumber) throws ClientException {
        CommonRequest request = new CommonRequest();
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
        request.putQueryParameter("SignName", "尾巴");
        // 短信模板ID
        request.putQueryParameter("TemplateCode","SMS_189760467");
         //生成一个六位数的验证码
        String code = String.valueOf(Math.random()).substring(2,8);
        System.out.println(code);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"sms_code\":\""+code+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object bizId = map.get("BizId");
        System.out.println("手机号:"+phoneNumber+",注册验证码发送短信成功");
        return code;
    }
}